//
//  SocialCell.m
//  SocialCell
//
//  Created by student10 on 2020/11/22.
//  Copyright © 2020 student10. All rights reserved.
//

#import "SocialCell.h"
#import "SocialTagListView.h"
#import "Masonry.h"
#import "NSArray+Sudoku.h"
#import "PublishViewController.h"
#import "HomeViewDemo.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <AFNetworking.h>

//设备的宽高
#define SCREENWIDTH       [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT      [UIScreen mainScreen].bounds.size.height

#define PICWIDTH          ([UIScreen mainScreen].bounds.size.width - 80.0)/3
#define PICHEIGHT         ([UIScreen mainScreen].bounds.size.width - 80.0)/3

@implementation SocialCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self configureView];
}

//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
//    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
//
//    if (self) {
//        [self configureView];
//    }
//
//    return self;
//}

- (id) init {
    return [self initWithPicnum:0 andCanEdit:NO andHasLike:NO andTagList:nil];
}

- (instancetype)initWithPicnum: (NSInteger)num andCanEdit:(BOOL)canEdit andHasLike:(BOOL) haslike andTagList:(NSArray*)tagArray{
    self = [super init];
    
    if (self) {
        self.picnum = num;
        self.haslike = haslike;
        self.canEdit = canEdit;
        self.tagArray = tagArray;
        [self configureView];
    }
    
    return self;
}

- (void)configureView {
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.userInteractionEnabled = YES;
    UITapGestureRecognizer *imageTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickCom:)];
    //[self.contentView addGestureRecognizer:imageTap2];
    
    CGFloat avatarButtonWidth = 32.0;
    
    self.avatarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.avatarButton.layer.cornerRadius = avatarButtonWidth / 2.0;
    self.avatarButton.clipsToBounds = YES;
    [self.avatarButton setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.avatarButton];
    
    [self.avatarButton addTarget:self action:@selector(onClickAva:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *view = [[UIView alloc] init];
    
    self.nicknameLabel = [[UILabel alloc] init];
    self.nicknameLabel.font = [UIFont systemFontOfSize:13.0];
    self.nicknameLabel.textColor = [UIColor blackColor];
    self.nicknameLabel.text = @"昵称";
    [view addSubview:self.nicknameLabel];
    
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.font = [UIFont systemFontOfSize:12.0];
    self.timeLabel.textColor = [UIColor grayColor];
    self.timeLabel.text = @"4-27";
    [view addSubview:self.timeLabel];
    
    [self.contentView addSubview:view];
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.font = [UIFont systemFontOfSize:15.0];
    self.contentLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.contentLabel];
    
    
    UIView* vp = [[UIView alloc] init];

//    if(self.picnum >= 1) {
//        (self.picArray[0]) = [[UIImageView alloc] init];
//        [vp addSubview:self.picArray[0]];
//    }
//    if (self.picnum >= 2) {
//        self.picArray[1] = [[UIImageView alloc] init];
//        [vp addSubview:self.picArray[1]];
//    }
//    if (self.picnum >= 3) {
//        self.picArray[2] = [[UIImageView alloc] init];
//        [vp addSubview:self.picArray[2]];
//    }
    self.picArray  = [NSMutableArray arrayWithCapacity:self.picnum];
    for (int i=0; i<self.picnum; i++) {
        UIImageView* temp = [[UIImageView alloc] init];
        [vp addSubview:temp];
        [self.picArray addObject:temp];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickPic:)];
        
        [temp setTag:i]; //set tag value
        [temp addGestureRecognizer:tap];
        [temp setUserInteractionEnabled:YES];
    }
    
    [self.contentView addSubview:vp];

//    [self.picArray mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(vp.mas_bottom).offset(15.0);
//        make.bottom.equalTo(vp.mas_bottom).offset(-15.0);
//        make.size.mas_equalTo(CGSizeMake(PICWIDTH, PICHEIGHT));
//        make.leading.equalTo(self.avatarButton.mas_leading);
//        make.trailing.equalTo(self.contentView.mas_trailing).offset(-20.0);
//    }];

     [self.picArray mas_distributeSudokuViewsWithFixedLineSpacing:10 fixedInteritemSpacing:10 warpCount:3 topSpacing:10 bottomSpacing:10 leadSpacing:10 tailSpacing:40];
    
//    if (self.picnum >= 1 ) {
//        [((UIImageView*)self.picArray[0]) mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(vp.mas_top).offset(0);
//            make.size.mas_equalTo(CGSizeMake(PICWIDTH, PICHEIGHT));
//            make.leading.equalTo(self.contentView.mas_leading).offset(20.0);
//        }];
//    }
//    if (self.picnum >= 2) {
//        [self.picArray[1] mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(vp.mas_top).offset(0);
//            make.size.mas_equalTo(CGSizeMake(PICWIDTH, PICHEIGHT));
//            make.leading.equalTo(self.contentView.mas_leading).offset(40.0+PICWIDTH);
//        }];
//    }
//    if (self.picnum >= 3) {
//        [self.picArray[2] mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(vp.mas_top).offset(0);
//            make.size.mas_equalTo(CGSizeMake(PICWIDTH, PICHEIGHT));
//            make.leading.equalTo(self.contentView.mas_leading).offset(60.0+2*PICWIDTH);
//        }];
//    }
    
    SocialTagListView *tag = [[SocialTagListView alloc]initWithFrame:CGRectMake(0, 300, self.contentView.frame.size.width, 0)];
//    tag.delegate_ = self;
    tag.backgroundColor = [UIColor clearColor];
    [tag setupSubViewsWithTitles:self.tagArray];
    [self.contentView addSubview:tag];
    [self.contentView bringSubviewToFront:tag];
    
    CGRect rect = tag.frame;
    rect.size.height = tag.contentSize.height;
    tag.frame = rect;
    
    UIView* vc = [[UIView alloc]init];
    UIImage *likeImage = [[UIImage imageNamed:@"timeline_icon_unlike@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    self.likeIconImage = [[UIImageView alloc] initWithImage:likeImage];
    if (self.haslike) {
        self.likeIconImage.tintColor = UIColor.systemRedColor;
    } else {
        self.likeIconImage.tintColor = UIColor.darkGrayColor;
    }
    
//    self.backgroundColor = [UIColor grayColor];
//    self.likeIconImage.clipsToBounds = YES;
    self.likeIconImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickLike:)];
    [self.likeIconImage addGestureRecognizer:imageTap];
    [self.contentView addSubview:self.likeIconImage];
//    [self.likeIconImage addTarget:self action:@selector(onClickLike:) forControlEvents:UIControlEventTouchUpInside];
    
    self.commentIconImage = [[UIImageView alloc] init];
    self.commentIconImage.image = [UIImage imageNamed:@"timeline_icon_comment@2x"];
    self.commentIconImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *imageTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickCom:)];
    [self.commentIconImage addGestureRecognizer:imageTap1];
    [self.contentView addSubview:self.commentIconImage];
    
    self.deleteIconImage = [[UIImageView alloc] init];
    self.deleteIconImage.image = [UIImage imageNamed:@"outline_delete_forever_black_24pt"];
    self.deleteIconImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *imageTap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickDelete:)];
    [self.deleteIconImage addGestureRecognizer:imageTap4];
    [self.contentView addSubview:self.deleteIconImage];
    if(self.canEdit == NO) {
        self.deleteIconImage.hidden = YES;
    }
    
    self.editIconImage = [[UIImageView alloc] init];
    self.editIconImage.image = [UIImage imageNamed:@"edit.jpg"];
    self.editIconImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *imageTap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickEdit:)];
    [self.editIconImage addGestureRecognizer:imageTap3];
    [self.contentView addSubview:self.editIconImage];
    if(self.canEdit == NO) {
        self.editIconImage.hidden = YES;
    }

    self.likeCountLabel = [[UILabel alloc] init];
    self.likeCountLabel.numberOfLines = 0;
    self.likeCountLabel.font = [UIFont systemFontOfSize:15.0];
    self.likeCountLabel.textColor = [UIColor blackColor];
    
    
    self.commentCountLabel = [[UILabel alloc] init];
    self.commentCountLabel.numberOfLines = 0;
    self.commentCountLabel.font = [UIFont systemFontOfSize:15.0];
    self.commentCountLabel.textColor = [UIColor blackColor];
    
//    vc.userInteractionEnabled = YES;
//    [vc addSubview:self.commentIconImage];
    [vc addSubview:self.commentCountLabel];
//    [vc addSubview:self.likeIconImage];
    [vc addSubview:self.likeCountLabel];
    
//    self.commentlabel  = [NSMutableArray arrayWithCapacity:self.commentnum];
//    for (int i=0; i<self.commentnum; i++) {
//        UILabel* la = [[UILabel alloc]init];
//
//        la.numberOfLines = 0;
//        la.font = [UIFont systemFontOfSize:15.0];
//        la.textColor = [UIColor blackColor];
//        la.backgroundColor = [UIColor grayColor];
//        [self.commentlabel addObject:la];
//
//        [vc addSubview:la];
//    }

    [self.contentView addSubview:vc];
    
    [self.avatarButton mas_makeConstraints:^(MASConstraintMaker *make) {
        // 步骤3：设置头像的约束，最重要的思想是：给头像的top与contentView.mas_top之间建立约束
        make.top.equalTo(self.contentView.mas_top).offset(27.0);
        // 设置size与leading（left）的约束，不解释
        make.size.mas_equalTo(CGSizeMake(avatarButtonWidth, avatarButtonWidth));
        make.leading.equalTo(self.contentView.mas_leading).offset(20.0);
    }];
    
    // nicknameLabel与timeLabel也是使用了自动撑开的思想
    [self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.equalTo(view);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.nicknameLabel.mas_leading);
        make.top.equalTo(self.nicknameLabel.mas_bottom).offset(3.0);
        make.bottom.equalTo(view.mas_bottom);
    }];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.avatarButton.mas_trailing).offset(10.0);
        make.trailing.lessThanOrEqualTo(self.contentView.mas_trailing).offset(-10.0);
        make.centerY.equalTo(self.avatarButton.mas_centerY);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        // 步骤4:设置评论的top与头像的bottom之间的约束
        make.top.equalTo(self.avatarButton.mas_bottom).offset(15.0);
        // 步骤5：设置评论的bottom与contentView.mas_bottom之间的约束
//        make.bottom.equalTo(self.contentView.mas_bottom).offset(-25.0);
        make.leading.equalTo(self.avatarButton.mas_leading);
        make.trailing.equalTo(self.contentView.mas_trailing).offset(-20.0);
    }];
    
    [vp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(15.0);
        if (self.picnum == 0) {
            make.bottom.equalTo(self.contentLabel.mas_bottom).offset(0);
        } else {
//            NSLog(@"cccc");
            make.bottom.equalTo(self.contentLabel.mas_bottom).offset(15.0 + PICHEIGHT*(self.picnum/3+1));
        }

        make.leading.equalTo(self.contentLabel.mas_leading);
        make.trailing.equalTo(self.contentView.mas_trailing);
        
    }];
    
//    [self.bar mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(vp.mas_bottom).offset(15.0);
//        make.bottom.equalTo(self.contentView.mas_bottom).offset(-15.0);
//        make.leading.equalTo(self.avatarButton.mas_leading);
//        make.trailing.equalTo(self.contentView.mas_trailing).offset(-20.0);
//    }];
    
    [tag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vp.mas_bottom);
//        self.tagArray.count / 3 * 40
        make.bottom.equalTo(vp.mas_bottom).offset(self.tagArray.count / 4 * 30 + 20);
        make.leading.equalTo(self.contentView.mas_leading);
        make.trailing.equalTo(self.contentView.mas_trailing);
    }];
    
    [vc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tag.mas_bottom).offset(10.0);
        // 步骤5：设置评论的bottom与contentView.mas_bottom之间的约束
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-45.0);
        make.leading.equalTo(self.avatarButton.mas_leading);
        make.trailing.equalTo(self.contentView.mas_trailing).offset(-20.0);
    }];
    
    [self.commentIconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vc.mas_top).offset(0);
        make.size.mas_equalTo(CGSizeMake(32.0, 32.0));
        make.leading.equalTo(self.avatarButton.mas_leading);
    }];
    [self.commentCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.commentIconImage.mas_centerY);
        make.left.mas_equalTo(self.commentIconImage.mas_right).offset(5.0);
    }];
    [self.likeIconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vc.mas_top).offset(0);
        make.size.mas_equalTo(CGSizeMake(32.0, 32.0));
        make.left.mas_equalTo(self.commentIconImage.mas_right).offset(65.0);
    }];
    [self.likeCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.likeIconImage.mas_centerY);
        make.left.mas_equalTo(self.likeIconImage.mas_right).offset(5.0);
    }];
    
    [self.editIconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vc.mas_top).offset(0);
        make.size.mas_equalTo(CGSizeMake(32.0, 32.0));
        make.trailing.equalTo(self.contentView.mas_trailing).offset(-20.0);
    }];
    [self.deleteIconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vc.mas_top).offset(0);
        make.size.mas_equalTo(CGSizeMake(32.0, 32.0));
        make.right.equalTo(self.editIconImage.mas_left).offset(-10.0);
    }];
    
//    [self.commentlabel mas_distributeViewsAlongAxis:MASAxisTypeVertical
//                                   withFixedSpacing:1.0
//                                        leadSpacing:0
//                                        tailSpacing:0
//     ];
    
//    for (int i=0; i<self.commentnum; i++) {
//        [self.commentlabel[i] mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self).with.offset(15.0);
//                    make.right.equalTo(self).with.offset(-15.0);
//                }];
//    }
    
//    _bigImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (SCREENHEIGHT - SCREENWIDTH) / 2, SCREENWIDTH, SCREENWIDTH)];
//    [_bigImageView setImage:self.likeIconImage.image];
//    [self.contentView addSubview:_bigImageView];
}

- (UIImageView *)bigImageView :(NSInteger)index {
    _bigImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (SCREENHEIGHT - SCREENWIDTH) / 2, SCREENWIDTH, SCREENWIDTH)];
    [_bigImageView setImage:((UIImageView*)self.picArray[index]).image];
    _bigImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissBigImage:)];
    [_bigImageView addGestureRecognizer:imageTap];
    [_bigImageView setTag:index];
    
    return _bigImageView;
}

- (UIView*)backView :(NSInteger)index {
    if (nil == _backView) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
        _backView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        _backView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissBigImage:)];
        [_backView addGestureRecognizer:tap];
        [_backView setTag:index];
    }
    
    [_backView setTag:index];
    return _backView;
}

- (void)onClickPic:(UIGestureRecognizer *)sender {
    
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 300)];
    aView.backgroundColor = [UIColor redColor];
    // 当前顶层窗口
    self.lastWindow = [[UIApplication sharedApplication].windows lastObject];
    // 添加到窗口
    
//    NSLog(@"点击了第%ld张图片", sender.view.tag);
    [self bigImageView:sender.view.tag];
    self.bigImageView.center = CGPointZero;
    
    // 动画到大图该有的大小
    [UIView animateWithDuration:0.3 animations:^{
        // 改变大小
        // 让大图从小图的位置和大小开始出现
        CGRect originFram = self.bigImageView.frame;
        self.bigImageView.frame = ((UIImageView*)self.picArray[sender.view.tag]).frame;
        [self.lastWindow addSubview:self.bigImageView];
        //    [_lastWindow addSubview:_bigImageView];
        
        self.bigImageView.frame = originFram;
        // 改变位置
        self.bigImageView.center = self.lastWindow.center;// 设置中心位置到新的位置
//        CGPoint cen = CGPointMake(SCREENWIDTH/2, SCREENHEIGHT/2);
//        self.bigImageView.center = cen;
//        self.bigImageView.center = CGPointMake(CGRectGetWidth(self.backgroundView.frame)/2, CGRectGetHeight(self.backgroundView.frame)/2);
        
//        self.bigImageView.frame = CGRectMake(0.0f, - rect.origin.y + SCREENWIDTH/2, originFram.size.width, originFram.size.height);
    }];
    
    
    [self backView:sender.view.tag];
    [_lastWindow addSubview:_backView];

    self.backView.frame = CGRectMake(0.0f, 0.0f, SCREENWIDTH, SCREENHEIGHT);
    
    [_lastWindow bringSubviewToFront:_bigImageView];
}

- (void)dismissBigImage:(UIGestureRecognizer *)sender {
    [self.backView removeFromSuperview];// 移除阴影
    
//     将大图动画回小图的位置和大小
    [UIView animateWithDuration:0.3 animations:^{
//         改变大小
        self.bigImageView.frame = ((UIImageView*)self.picArray[sender.view.tag]).frame;
//         改变位置
    
        self.bigImageView.center = CGPointZero;// 设置中心位置到新的位置
    }];
    
    // 延迟执行，移动回后再消灭掉
    double delayInSeconds = 0.3;
    __block SocialCell* bself = self;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [bself.bigImageView removeFromSuperview];
        bself.bigImageView = nil;
        bself.backView = nil;
    });
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:NO animated:animated];
    // Configure the view for the selected state
}

- (void)onClickAva:(id)sender {
    NSLog(@"select ava");
}

- (void)onClickLike:(id)sender
{
    NSLog(@"like");
    NSDictionary *postParam = @{@"isContent":@YES, @"isComment":@NO, @"isReply":@NO};
    NSString *contentID = self.contentID;
    NSString *URLString = [NSString stringWithFormat:@"%@%@", @"http://172.18.178.56/api/like/", contentID];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    if(self.haslike) {
        [manager PATCH:URLString parameters:postParam success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"取消点赞%@",responseObject);
            if ([responseObject[@"State"]  isEqual: @"success"]) {
                self.likeCountLabel.text = [NSString stringWithFormat:@"%d", self.likeCountLabel.text.intValue - 1];
                self.haslike = !self.haslike;
                self.likeIconImage.tintColor = UIColor.darkGrayColor;
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"failed");
        }];
    } else {
        [manager POST:URLString parameters:postParam success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"点赞%@",responseObject);
            if ([responseObject[@"State"]  isEqual: @"success"]) {
                self.likeCountLabel.text = [NSString stringWithFormat:@"%d", self.likeCountLabel.text.intValue + 1];
                self.haslike = !self.haslike;
                self.likeIconImage.tintColor = UIColor.systemRedColor;
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"failed");
        }];
    }
}

- (void)onClickEdit:(UITapGestureRecognizer *)tap
{
    NSLog(@"Edit");
    NSString *URLString = [NSString stringWithFormat:@"%@%@", @"http://172.18.178.56/api/content/all/", self.contentID];
    PublishViewController *VC = [[PublishViewController alloc] initWithURLString:URLString];
    [VC.titleField setText:[self.contentLabel.text componentsSeparatedByString:@"\n"].firstObject];
    [VC.detailView setText:self.contentLabel.text];
    [VC setHidesBottomBarWhenPushed:YES];
    [[self viewController].navigationController pushViewController:VC animated:YES];
}

- (void)onClickDelete:(UITapGestureRecognizer *)tap
{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Confirm"
                                   message:@"确认删除该内容吗？"
                                   preferredStyle:UIAlertControllerStyleAlert];
     
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive
       handler:^(UIAlertAction * action) {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSString *URLString = [NSString stringWithFormat:@"%@%@", @"http://172.18.178.56/api/content/", self.contentID];
        [manager DELETE:URLString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if ([responseObject[@"State"]  isEqual: @"success"]) {
                NSLog(@"删除成功");
                HomeViewDemo *VC = (HomeViewDemo *)[self viewController];
                UITableView *tableView = (UITableView *)(self.superview);
                NSIndexPath *indexPath = [tableView indexPathForCell:self];
                [VC removeObjectFromIndexpath:indexPath];
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"删除失败");
        }];
    }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
       handler:^(UIAlertAction * action) {
        NSLog(@"Cancel");
    }];
     
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [[self viewController] presentViewController:alert animated:YES completion:nil];
}


- (void)onClickCom:(UITapGestureRecognizer *)tap
{
    NSLog(@"Comment");
}

- (void)onClickStar:(UITapGestureRecognizer *)tap
{
    NSLog(@"star");
}

- (void)onClickDownload:(UITapGestureRecognizer *)tap
{
    NSLog(@"Download");
}

#pragma mark 获得当前view的控制器
- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController
                                          class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

@end
